<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ include file="/WEB-INF/jsp/component/common.jsp"%>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge"></meta>
	<title>设备管理</title>
	<link rel="stylesheet" href="${frames}/css/lib/bootstrap.min.css" type="text/css" type="text/css">
	<link rel="stylesheet" href="${frames}/css/lib/dataTables.bootstrap.css" type="text/css">
	<link rel="stylesheet" href="${newframe}/css/base/base.css" type="text/css">
	<link rel="stylesheet" href="${assets}/css/views/department/departmentMgr.css">
	<%@ include file="/WEB-INF/jsp/component/commonBottom.jsp"%>
	<style>
		#upload_instrument {
			position: relative; /* 保证子元素的定位 */
			background: #eee;
			border: 1px solid #ccc;
			text-align: center;
			cursor: pointer;
		}

		#text {
			display: inline-block;
			margin-top: 5px;
			color: #666;
			font-family: "黑体";
			font-size: 18px;
		}

		#file {
			display: block;
			position: absolute;
			top: 0;
			left: 0;
			width: 100%; /* 宽高和外围元素保持一致 */
			height: 100%;
			opacity: 0;
			-moz-opacity: 0; /* 兼容老式浏览器 */
			filter: alpha(opacity = 0); /* 兼容IE */
		}
		.wrap{
			position:relative;
		}
		.loading{
			width:100%;
			height:100%;
			position:absolute;
			left:0;
			top:0;
			/* z-index:-3; */

		}
		.loadingImg{
			width:50px;
			height:50px;
			position:absolute;
			top:330px;
			left:50%;
			margin-left:-25px;
		}
		#mytool{
			float:right
		}
	</style>
	<script>
        function addInstr() {
            window.parent.showNewModelIframe(
                "${URL_BASE_TOUPDATEINSTRUMENT}?method=1",750, 400);
        }
        function updateInstr() {
            var checkedArray = getCheckedArray();
            var CheckedValue = getCheckedValue()
            if (checkedArray.length != 1) {
                alert("请勾选一条数据修改");
                return;
            }
            var url = encodeURI(encodeURI("${URL_BASE_TOUPDATEINSTRUMENT}?id="
                + checkedArray[0] + "&method=2" + '&name=' + CheckedValue))
            window.parent.showNewModelIframe(url, 750, 400);
        }
        function deleteInstr() {
            var checkedArray = getCheckedArray();
            var ids = "";
            if (checkedArray.length == 0) {
                alert("请勾选至少一项数据");
                return;
            } else {
                for (var i = 0; i < checkedArray.length; i++) {
                    ids += checkedArray[i] + ",";
                }
                ids = ids.substring(0, ids.length - 1);
            }
            var r = confirm("确定要删除勾选的数据吗?");
            if (r == true) {
                $.ajax({
                    url : '${URL_BASE_DELETEINSTRUMENT}',//删除地址
                    dataType : 'json',
                    type : 'post',
                    data : {
                        ids : ids
                    },
                    async : false, //默认为true 异步
                    error : function() {
                        alert('error');
                    },
                    success : function(data) {

                        if (!data.success) {
                            alert("刪除失敗，失敗原因：" + data.msg);
                        }
                        refreshTable();
                    }
                });
            }
        }
        function downTemplate() {
            window.location.href = "${newframe}/upload/template/instrumentTemplate.xlsx";
        }
        $(function() {
            $("#importModal").click(function() {
                ajaxFileUpload();
            });

            $('#fileModal').on('hide.bs.modal', function() {
                var xx = $("#file").val('');
            });
        });
        function showImage() {
            var val = $("#file").val();

            if (val == "") {
                alert("请选择要上传的文件！");
                return false;
            }
            var stuff = val.match(/^(.*)(\.)(.{1,8})$/)[3];
            if (stuff != "xls" && stuff != "xlsx" && stuff != "txt") {
                alert("文件类型不正确，请选择excel文件！");
                return false;
            }
            $(".content").html(val);
            $('#fileModal').modal('show');
        }
        function ajaxFileUpload() {
            $(".loading").show();
            //隐藏上传选项
            $.ajaxFileUpload({
                url : '${ctx}/instrument/upload', //用于文件上传的服务器端请求地址
                secureuri : false, //是否需要安全协议，一般设置为false
                fileElementId : 'file', //文件上传域的ID
                dataType : 'json', //返回值类型 一般设置为json
                success : function(data, status) //服务器成功响应处理函数
                {
                    if(data.success==true){
                        alert(data.msg);
                        refreshTable();
                    }else{
                        alert(data.msg);
                    }
                    $(".loading").hide();

                },
                error : function(data, status, e)//服务器响应失败处理函数
                {
                    alert(e);
                }
            })
            return false;
        }
	</script>
</head>
<body>

<div class="wrap">
	<%@ include file="../breadline.jsp" %>
	<div class="content">
		<h1>设备管理</h1>
		<div class="form">
			<label>设备名称:</label>
			<select id="instrumentType" name="instrumentType" class="SN">
				<medi:configOpt name="instrumentName"/>
			</select>
			<label>设备SN:</label>
			<input id="instrumentSN" name="instrumentSN" type="text" class="SN" placeholder="设备SN">
			<label>设备状态:</label> <select
				id="instrumentStatus" name="instrumentStatus" class="SN">
			<medi:configOpt name="instrumentStatus" />
		</select>
			<button class="search" id="query" onclick="refreshTable();">搜索</button>
		</div>
		<div id="tablepart">
			<table id="example" class="display" cellspacing="10" width="100%" >
				<thead>
				<tr style="background: #e6e6e6;">
					<th><input id="all_id" type="checkbox"></th>
					<th style="width:80px;">序号</th>
					<th>设备名称</th>
					<th>设备SN</th>
					<%--<th>设备型号</th>--%>
					<%--<th>设备品牌</th>--%>
					<th>设备状态</th>
					<th>所属医院</th>
				</tr>
				</thead>
			</table>
		</div>
	</div>
	<div class="loading" style="display: none;">
		<img src="${newframe }/images/slidebar/loading.gif" class="loadingImg">
	</div>
</div>
<script>
    var table;
    var ListNum = 0;
    var bb = 0;
    var arr2 = [];
    var arr1 = [];
    function initTable() {
        table = $('#example')
            .DataTable(
                {
                    "dom" : "<'row'<'#mytool.col-xs-10'><'col-xs-6'f>r>"
                    + "t"
                    + "<'row pageNum'<p>>",
                    "bFilter" : false,//去掉搜索框
					"sLengthMenu":[10],
                    "bAutoWidth" : true, //自适应宽度
                    "bInfo":false,
                    "sPaginationType" : "full_numbers",
                    "ordering" : false,//是否允许Datatables开启排序
                    "bProcessing" : true,
                    "bServerSide" : true,
                    "sScrollX":"100%",
                    "sAjaxSource" : "${URL_BASE_SEARCHINSTRUMENT}",
                    "sAjaxDataProp" : "dataList",//查询后，返回的集合
                    "fnServerData" : function(sSource, aoData,
                                              fnCallback) { //查询条件
                        aoData.push({
                            "name" : "instrumentType",
                            "value" : $("#instrumentType").val()
                        }, {
                            "name" : "instrumentStatus",
                            "value" : $("#instrumentStatus").val()
                        }, {
                            "name" : "instrumentSN",
                            "value" : $("#instrumentSN").val()
                        });
                        $.ajax({
                            "dataType" : 'json',
                            "url" : sSource,
                            "type" : 'post',
                            "data" : aoData,
                            "success" : function(resp){
                                console.log(resp)
                                var page = parseInt(resp.iTotalRecords / 10) + 3;
                                if(page >= 9){
                                    page = 9;
                                }
                                var W = page * 45
                                $('.pageNum').width(W + 'px');
                                if( ListNum == 0 ){
                                    bb = 1
                                }
                                var arr3 = []
                                for ( var a = 0; a < resp.iTotalDisplayRecords; a++ ){
                                    arr3.push(a+1)
                                }
                                var arr1 = arr3.slice((bb-1)*10,bb*10);
								console.log(arr1)
                                for( var a = 0 ; a < arr1.length; a++ ){
                                    resp.dataList[a].oldNumber = arr1[a]
                                }
                                fnCallback(resp)
                                var alis = $('.pagination li')
                                var lisa = alis.slice(1,alis.length-1)
                                for( var a = 0; a < lisa.length; a++ ){
                                    $(lisa[a]).on('click',function(){
                                        bb = $(this).find('a').text()
                                    })
                                }
                                $(alis[0]).on('click',function(){
                                    if( bb != 0 ){
                                        bb --;
                                    }
                                })
                                $(alis[alis.length-1]).on('click',function(){
                                    if( bb < lisa.length ){
                                        bb++;
                                    }
                                })
                                ListNum++;
                            }
                        });

                    },

                    "aoColumns" : [
                        {
                            "targets" : 0,
                            "data" : null,
                            "fnCreatedCell" : function(nTd,
                                                       sData, oData, iRow, iCol) {
                                $(nTd)
                                    .html(
                                        ''
                                        + '&nbsp;&nbsp;<input name="id" id = "'+oData.instrumentId + '" value="'
                                        + oData.instrumentType
                                        + '" type="checkbox"><label for='+oData.instrumentId+'></label>'
                                        + '');
                            }
                        },
                        {
                            "mDataProp" : "oldNumber"
                        },
                        {
                            "targets": 2,//编辑
                            "data": null,
                            "fnCreatedCell" : function(nTd, sData, oData, iRow, iCol) {

                                $(nTd).html('<a class="departmentDetail" href="#" style="color: #333333;border-bottom:#35acfd dashed 1px;">'+ oData.instrumentType +'</a>');
                            }

                        },
//                        {
//                            "mDataProp" : "instrumentSN"
//                        },
//                        {
//                            "mDataProp" : "instrmentVersion"
//                        },
                        {
                            "mDataProp" : "instrumentSN"
                        },
                        {
                            "targets" : 6,
                            "data" : null,
                            "fnCreatedCell" : function(nTd,
                                                       sData, oData, iRow, iCol) {
                                var configs = <medi:configJson name="instrumentStatus"/>;
                                $(nTd)
                                    .html(
                                        !configs[sData.instrumentStatus] ? ""
                                            : configs[sData.instrumentStatus]);
                            }
                        }, {
                            "mDataProp" : "hospShortName"
                        } ],
                    "oLanguage" : {//插件的汉化
                        "sLengthMenu" : '',
                        "sZeroRecords" : "抱歉， 没有找到",
                        "sInfoEmpty" : "没有数据",
                        "sInfoFiltered" : "(从 _MAX_ 条数据中检索)",
                        "oPaginate" : {
                            "sFirst" : "",
                            "sPrevious" : "<b>上一页<b>",
                            "sLast" : "",
                            "sNext" : "<i>下一页</i>"

                        },
                        "sZeroRecords" : "没有检索到数据",
                        "sProcessing" : "<img src='' />",
                        "sSearch" : "搜索"
                    },
                    "initComplete" : function() {
                        <shiro:hasPermission name="instrument:delete">
                        $("#mytool")
                            .append(
                                '<button id="delete_butt" type="button" onclick="deleteInstr()" style="float:right">删除</button>');
                        </shiro:hasPermission>
                        <shiro:hasPermission name="instrument:update">
                        $("#mytool")
                            .append(
                                '<button id="update_butt" type="button" onclick="updateInstr()" style="float:right">修改</button>');
                        </shiro:hasPermission>
                        <shiro:hasPermission name="instrument:add">
                        $("#mytool")
                            .append(
                                '<button id="add_butt" type="button" onclick="addInstr()" style="float:right">新增</button>');
                        </shiro:hasPermission>
                    }
                });
    }
    function getCheckedArray() {
        var checkedArray = [];
        $("input[name='id']").each(function() {
            if (this.checked) {
                checkedArray.push(this.id);
            }
        });
        return checkedArray;
    }
    function getCheckedValue() {
        var checkedValue = [];
        $("input[name='id']").each(function() {
            if (this.checked) {
                checkedValue.push(this.value);
            }
        });
        return checkedValue[0];
    }
    $(document).ready(function() {
        initTable();
        var table = $('#example').DataTable();
        var params = {};
        $('#example tbody').on( 'click', 'tr td a', function () {
            var Otr = $(this).parents('td').parents('tr');
            params = table.rows(Otr).data()[0];
            $.ajax({
                url:"${URL_SCREENING_GETBREADLINE}",
                dataType:'json',
                type:'post',
                success:function(data){
                    breadLine = JSON.parse(data.data)
                    var url = '${URL_BASE_INSTRUMENT_TOINFO}?departmentId=' + params.instrumentId
                    breadLine.push({
                        name:'设备详情',
                        href:url
                    })
                    $.ajax({
                        url:'${URL_SCREENING_SAVEBREADLINE}',
                        data:{
                            breadLine:JSON.stringify(breadLine)
                        },
                        dataType:'json',
                        type:'post',
                        success:function(data){
                            parent.showSrc('${URL_BASE_INSTRUMENT_TOINFO}?departmentId=' + params.instrumentId)
                        }
                    })
                }
            })
        });
    });
</script>
</body>
</html>